package com.ruoyi.system.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.system.domain.SysTemplate;
import com.ruoyi.system.service.ISysTemplateService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 租户权限Controller
 * 
 * @author ruoyi
 * @date 2021-02-02
 */
@RestController
@RequestMapping("/template")
public class SysTemplateController extends BaseController {
    @Autowired
    private ISysTemplateService sysTemplateService;

    /**
     * 查询租户权限列表
     */
    @PreAuthorize(hasPermi = "system:template:list")
    @GetMapping("/list")
    public TableDataInfo list(SysTemplate sysTemplate) {
        startPage();
        List<SysTemplate> list = sysTemplateService.queryList(sysTemplate);

        return getDataTable(list);
    }

    /**
     * 导出租户权限列表
     */
    @PreAuthorize(hasPermi = "system:template:export")
    @Log(title = "租户权限", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysTemplate sysTemplate) throws IOException
    {
        List<SysTemplate> list = sysTemplateService.queryList(sysTemplate);
        ExcelUtil<SysTemplate> util = new ExcelUtil<SysTemplate>(SysTemplate.class);
        util.exportExcel(response, list, "template");
    }

    /**
     * 获取租户权限详细信息
     */
    @PreAuthorize(hasPermi = "system:template:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(sysTemplateService.getById(id));
    }

    /**
     * 新增租户权限
     */

    @PreAuthorize(hasPermi = "system:template:add")
    @Log(title = "租户权限", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysTemplate sysTemplate)
    {
        sysTemplateService.insertSysTemplate(sysTemplate);

        return toAjax(1);
    }

    /**
     * 修改租户权限
     */
    @PreAuthorize(hasPermi = "system:template:edit")
    @Log(title = "租户权限", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysTemplate sysTemplate)
    {
            sysTemplateService.updateSysTemplate(sysTemplate);
        return toAjax(sysTemplateService.updateById(sysTemplate) ? 1 : 0);
    }

    /**
     * 删除租户权限
     */
    @PreAuthorize(hasPermi = "system:template:remove")
    @Log(title = "租户权限", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        sysTemplateService.removeSysTemplateByIds(Arrays.asList(ids));
        return toAjax(1);
    }
}
